<template>
  <view class="page-container">
    <!-- 顶部导航栏 -->
    <view class="top-bar">
      <view class="title">创建投票</view>
    </view>

    <!-- 核心投票类型 -->
    <view class="vote-cards">
      <view class="card green" @click="handleVoteType('general')">
        <view class="icon-box">
          <image src="/static/img/tongyong.png" mode="aspectFit" class="icon" />
        </view>
        <view class="card-content">
          <view class="card-title">通用投票</view>
          <view class="card-desc">支持图片视频音频等比赛评比等场景的通用投票模式。</view>
        </view>
        <view class="arrow">></view>
      </view>

      <view class="card blue" @click="handleVoteType('general')">
        <view class="icon-box">
          <image src="/static/img/lantu.png" mode="aspectFit" class="icon" />
        </view>
        <view class="card-content">
          <view class="card-title">图文投票</view>
          <view class="card-desc">海选/优秀员工/先进评比/歌唱舞蹈/萌宝/摄影/书法数百场景支持</view>
        </view>
        <view class="arrow">></view>
      </view>

      <view class="card red" @click="handleVoteType('general')">
        <view class="icon-box">
          <image src="/static/img/shitou.png" mode="aspectFit" class="icon" />
        </view>
        <view class="card-content">
          <view class="card-title">视频投票</view>
          <view class="card-desc">图文投票基础上增加视频展示功能，支持高清视频播放。</view>
        </view>
        <view class="arrow">></view>
      </view>
    </view>

    <!-- 投票专题模块 - 改为动态加载 -->
    <view class="topic-section" v-if="topics.length > 0">
      <view class="section-title">投票专题</view>
      <view class="topic-grid">
        <!-- 使用v-for动态渲染专题卡片 -->
        <view 
          class="topic-card" 
          :class="topic.colorClass" 
          @click="handleTopic(topic.id)"
          v-for="topic in topics" 
          :key="topic.id"
        >
          <view class="topic-header">
            <view class="topic-title">{{ topic.title }}</view>
            <view class="arrow">></view>
          </view>
          <view class="topic-desc">{{ topic.description }}</view>
        </view>
      </view>
    </view>
    
    <!-- 加载中或无数据时的提示 -->
    <view class="loading-tip" v-else>
      <view v-if="isLoading">加载投票专题中...</view>
      <view v-else>暂无投票专题数据</view>
    </view>
  </view>
  
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

const handleCreate = () => {
  uni.navigateTo({ url:'/pages/createVote/newVote' });
};

const handleVoteType = (type) => {
  if (type === 'general') {
     uni.navigateTo({ url:'/pages/createVote/detailedVote' });
  }
  // 处理投票类型点击逻辑
  console.log('选择投票类型:', type);
};

const handleTopic = (topicId) => {
  // 处理专题投票点击逻辑
  console.log('选择投票专题ID:', topicId);
};

// 定义数据状态
const topics = ref([]);
const isLoading = ref(true);

// 模拟从数据库获取数据的函数
const fetchTopicsFromDatabase = async () => {
  try {
    // 这里应该是实际的API调用，比如使用axios获取数据
    // 此处用模拟数据代替
    await new Promise(resolve => setTimeout(resolve, 800)); // 模拟网络延迟
    
    // 模拟从数据库获取的投票专题数据
    const mockTopics = [
      { 
        id: 'gov', 
        title: '机关单位投票', 
        description: '党政机关单位投票评选',
        colorClass: 'red'
      },
      { 
        id: 'photo', 
        title: '摄影大赛', 
        description: '摄影自拍比赛评比',
        colorClass: 'purple'
      },
      { 
        id: 'school-class', 
        title: '学校/班级投票', 
        description: '中小学校班级风采投票',
        colorClass: 'blue'
      },
      { 
        id: 'campus', 
        title: '校园投票', 
        description: '大学高校活动人物评选',
        colorClass: 'blue-dark'
      },
      { 
        id: 'product-display', 
        title: '产品陈列大赛', 
        description: '产品陈列比赛投票',
        colorClass: 'green'
      },
      { 
        id: 'spokesperson', 
        title: '代言人评选', 
        description: '产品/企业代言人征集',
        colorClass: 'pink'
      },
      { 
        id: 'baby', 
        title: '萌宝萌娃大赛', 
        description: '萌宝萌娃评选投票',
        colorClass: 'red-light'
      },
      { 
        id: 'calligraphy', 
        title: '书法大赛', 
        description: '书法书画作品评选投票',
        colorClass: 'dark-blue'
      }
    ];
    
    topics.value = mockTopics;
  } catch (error) {
    console.error('获取投票专题数据失败:', error);
  } finally {
    isLoading.value = false;
  }
};

// 组件挂载时获取数据
onMounted(() => {
  fetchTopicsFromDatabase();
});
</script>

<style scoped>
/* 原有样式保持不变 */
.page-container {
  padding: 32rpx;
  font-family: 'PingFang SC', sans-serif;
}

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 48rpx;
}

.title {
  font-size: 40rpx;
  font-weight: 600;
  color: #333;
  margin: auto;
}

.vote-cards .card {
  display: flex;
  align-items: center;
  padding: 48rpx;
  border-radius: 32rpx;
  margin-bottom: 32rpx;
  color: white;
  transition: transform 0.2s;
}

.vote-cards .card:active {
  transform: scale(0.98);
}

.green { background: linear-gradient(135deg, #64d488, #44c46c); }
.blue { background: linear-gradient(135deg, #64b8d4, #449fc4); }
.red { background: linear-gradient(135deg, #d46464, #c44444); }

.icon-box {
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 48rpx;
}

.card-content .card-title {
  font-size: 36rpx;
  margin-bottom: 16rpx;
}

.card-content .card-desc {
  font-size: 28rpx;
  line-height: 1.5;
}

.arrow {
  font-size: 48rpx;
  font-weight: 600;
}

.topic-section {
  margin-top: 64rpx;
}

.section-title {
  text-align: center;
  font-size: 36rpx;
  font-weight: 600;
  color: #333;
  margin-bottom: 48rpx;
}

.topic-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 32rpx;
}

.topic-card {
  padding: 36rpx;
  border-radius: 24rpx;
  color: white;
  transition: transform 0.2s;
}

.topic-card:active {
  transform: scale(0.98);
}

.topic-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12rpx;
}

.topic-header .topic-title {
  font-size: 32rpx;
  margin: 0;
}

.topic-card .topic-desc {
  font-size: 28rpx;
  color: rgba(255, 255, 255, 0.8);
  margin: 0;
}

.red { background: #d43f3a; }
.purple { background: #6f42c1; }
.blue { background: #337ab7; }
.blue-dark { background: #2e6da4; }
.green { background: #5cb85c; }
.pink { background: #d9534f; }
.red-light { background: #f0ad4e; }
.dark-blue { background: #292b2c; }

/* 新增加载提示样式 */
.loading-tip {
  text-align: center;
  padding: 40rpx;
  color: #666;
  font-size: 32rpx;
}
</style>